<!DOCTYPE html>
<html>
<head>
<script src="../../../resources/js-test.js"></script>
<script src="../resources/common.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>

<script>
description("Test importing an EC key for ECDSA.");

jsTestIsAsync = true;

var extractable = true;

var publicKeyJSON = {
    "kty": "EC",
    "crv": "P-256",
    "x": "fBEMZtz9qAf25p5F3bPHT2mhSE0gPo3Frajpqd18s8c",
    "y": "DfRImG5RveXRV2-ZkB-cLGqAakf9kHZDpyuDVZfvyMY"
};

var privateKeyJSON = {
    "kty": "EC",
    "crv": "P-256",
    "d": "H-M5UMX0YRJK6ZLCvf3xxzsWFfVxvVZ-YNGaofSM30I",
    "x": "fBEMZtz9qAf25p5F3bPHT2mhSE0gPo3Frajpqd18s8c",
    "y": "DfRImG5RveXRV2-ZkB-cLGqAakf9kHZDpyuDVZfvyMY"
};

var data = asciiToUint8Array("Hello, world!");

debug("Importing a public key...");
crypto.subtle.importKey("jwk", publicKeyJSON, { name: "ECDSA", namedCurve: "P-256" }, extractable, ["verify"]).then(function(result) {
    publicKey = result;
    debug("\nImporting a private key...");
    return crypto.subtle.importKey("jwk", privateKeyJSON, { name: "ECDSA", namedCurve: "P-256" }, extractable, ["sign"]);
}).then(function(result) {
    privateKey = result;
    debug("\nSigning some text...");
    return crypto.subtle.sign({ name: "ECDSA", hash: { name: "SHA-1" } }, privateKey, data);
}).then(function(result) {
    signature = result;

    debug("\nVerifying the signature...");
    return crypto.subtle.verify({ name: "ECDSA", hash: { name: "SHA-1" } }, publicKey, signature, data);
}).then(function(result) {
    verificationResult = result;
    shouldBe("verificationResult", "true");
    debug("\nVerifying a bad signature...");
    return crypto.subtle.verify({ name: "ECDSA", hash: { name: "SHA-1" } }, publicKey, new Uint8Array(256), data);
}).then(function(result) {
    verificationResult = result;
    shouldBe("verificationResult", "false");
}).then(finishJSTest, failAndFinishJSTest);
</script>

</body>
</html>
